package com.example.car.service.Impl;

import com.example.car.entity.*;
import com.example.car.mapper.MemberMapper;
import com.example.car.params.UserParams;
import com.example.car.service.UserService;
import com.example.car.vo.UserVo;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.mongodb.core.query.Criteria;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

@Service
public class UserServiceImpl implements UserService {

    @Autowired
    private MemberMapper memberMapper;

    @Override
    public List<User> getCustomerList(UserParams memberParams) {
        Criteria criteria = new Criteria();

        String name = memberParams.getName();
        if(!StringUtils.isBlank(name)) {
            criteria.where("name").regex(".*?\\" + name + ".*");
        }

        List<User> list = memberMapper.getCustomerList(criteria, memberParams.getPage(), memberParams.getLimit());

        return list;
    }

    @Override
    public User login(User user) {
        return memberMapper.login(user);
    }

    @Override
    public Role getUserRoleByUserId(String userId) {
        return memberMapper.getUserRoleByUserId(userId);
    }

    @Override
    public User getUserByUserId(String userId) {
        User user = memberMapper.getUserByUserId(userId);
        return user;
    }

    @Override
    public List<Router> getRouterAddr(String role) {

        // 获取角色id
        String roleId = memberMapper.getRoleId(role);

        // 获取路由id
        List<RouterRole> routerRoles = memberMapper.getRouterId(roleId);
        List<String> collect = routerRoles.stream().map(item -> {
            return item.getRouterId();
        }).collect(Collectors.toList());

        // 获取路由
        List<Router> routerList = memberMapper.getRouter(collect);

        return routerList;

    }

    @Override
    public void updateUser(UserVo userVo) {
        memberMapper.updateUser(userVo);
    }

    @Override
    public String checkPhoneAndAccount(String phone, String account) {
        String result = memberMapper.checkPhoneAndAccount(phone, account);

        return result;
    }

    @Override
    public boolean save(User user) {
        return memberMapper.save(user);
    }

    @Override
    public String deleteUserById(String userId) {

        User userByUserId = memberMapper.getUserByUserId(userId);
        if (userByUserId == null) {
            return "用户不存在";
        }
        memberMapper.deleteUserById(userId);
        return null;
    }
}
